package com.example.bookmanager;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.xuyang.other.BookBean;
import com.xuyang.other.Flags;
import com.xuyang.other.ProgressDialog;

public class BookDetailsActivity extends Activity {
	private TextView txtName,txtWriter,txtIntroduce,txtRemarks,txtLanguage;
	private BookBean book;
	private LinearLayout layoutBorrow;
	private TextView txtBorrow;
	private ImageView imgFinish;
	private RequestQueue mQueue;
	private AlertDialog dialog;
	private int position;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mQueue=Volley.newRequestQueue(this);
		book=(BookBean) getIntent().getExtras().getSerializable("book");
		position=getIntent().getExtras().getInt("position");
		setContentView(R.layout.activity_book_details);
		findViewsById();
		init();
	}
	private void init() {
		txtName.setText(book.getName());
		txtWriter.setText(book.getWriter());
		txtIntroduce.setText(book.getIntroduce());
		txtRemarks.setText(book.getRemarks());
		txtLanguage.setText(book.getLanguages());
		if (book.getState()==1) {
			layoutBorrow.setBackgroundResource(R.drawable.selector_bookdetails_borrow);
			txtBorrow.setText("马上预阅");
		}
		//点击预阅功能代码
		layoutBorrow.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					//如果点击时发现当前状态是0时，不做任何处理
					if (book.getState()==0) {
						return ;
					}
					if (Flags.UserName.equals("")) {
						Toast.makeText(getApplicationContext(), "请先登录", 0).show();
					}else {
						String url=Flags.BORROW_BOOK.replace("BOOKID", String.valueOf(book.getId())).replace("USERNAME", Flags.UserName);
						final StringRequest request=new StringRequest(url,
						new Response.Listener<String>() {
							@Override
							public void onResponse(String result) {
								if (result.equals("borrowed")) {
									Toast.makeText(getApplicationContext(), "该书已被预阅", 0).show();
									book.setState(0);
									layoutBorrow.setBackgroundColor(Color.parseColor("#becdcb"));
									txtBorrow.setText("不可预阅");
									Intent intent=new Intent();
									intent.putExtra("book", book);
									intent.putExtra("position", position);
									setResult(10086, intent);
								}else if (result.equals("error")) {
									Toast.makeText(getApplicationContext(), "服务器出错，请重试", 0).show();
								}else if (result.equals("ok")) {
									Toast.makeText(getApplicationContext(), "预阅成功", 0).show();
									book.setState(0);
									layoutBorrow.setBackgroundColor(Color.parseColor("#becdcb"));
									txtBorrow.setText("不可预阅");
									Intent intent=new Intent();
									intent.putExtra("book", book);
									intent.putExtra("position", position);
									setResult(10086, intent);
								}
								if (dialog!=null) {
									dialog.dismiss();
								}
							}
						},
						new Response.ErrorListener() {
							@Override
							public void onErrorResponse(VolleyError error) {
								Toast.makeText(getApplicationContext(), "网络连接异常", 0).show();
							}
						});
						mQueue.add(request);
						//出现提示框，提示用户进度
						dialog=new ProgressDialog(BookDetailsActivity.this).getDialog();
						dialog.setOnKeyListener(new OnKeyListener() {
							@Override
							public boolean onKey(DialogInterface d, int keyCode, KeyEvent event) {
								 if (keyCode == KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN) {
									 	new AlertDialog.Builder(BookDetailsActivity.this)
									 	.setTitle("提示")
									 	.setMessage("确定取消预阅？")
									 	.setNegativeButton("确定", new DialogInterface.OnClickListener() {
											@Override
											public void onClick(DialogInterface a, int which) {
												request.cancel();
												dialog.dismiss();
											}
										})
										.setPositiveButton("取消", null)
										.create()
										.show();
							            return false;
							        }
								return false;
							}
					});
					}
				}
			});
		//退出按钮
		imgFinish.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	private void findViewsById() {
		txtName=(TextView) findViewById(R.id.txt_book_details_name);
		txtWriter=(TextView) findViewById(R.id.txt_book_details_writer);
		txtIntroduce=(TextView) findViewById(R.id.txt_book_details_introduce);
		txtRemarks=(TextView) findViewById(R.id.txt_book_details_remarks);
		layoutBorrow=(LinearLayout) findViewById(R.id.layout_bookdetails_borrow);
		txtBorrow=(TextView) findViewById(R.id.txt_bookdetails_borrow);
		imgFinish=(ImageView) findViewById(R.id.img_bookdestails_finish);
		txtLanguage=(TextView) findViewById(R.id.txt_book_details_language);
	}
	
}
